package org.openwebtop.sunshine.action.history;

import java.util.List;

import org.openwebtop.sunshine.bo.DashboardBO;
import org.openwebtop.sunshine.model.Component;
import org.openwebtop.sunshine.subversion.SubversionClient;
import org.openwebtop.sunshine.subversion.SubversionClientFactory;
import org.openwebtop.sunshine.subversion.model.RevisionInfo;

public class HistoryListAction {
	private final static int DEFAULT_PAGE_SIZE = 10;
	private final static String LIST = "list";
	private DashboardBO dashboardBO;
	private SubversionClientFactory subversionClientFactory;

	// Params
	private int id;
	private long from;
	private long to;
	private int size;

	// Getter
	private Component component;
	private List<RevisionInfo> revisionInfoList;

	public String list() throws Exception {
		component = dashboardBO.getComponent(id);

		if (component != null) {
			final SubversionClient client = subversionClientFactory.getSubversionClient(component);
			final long lastestRevision = client.getLastestRevision();

			if (to <= 0 || to > lastestRevision) {
				to = 0;
			}

			if (from <= 0 || from > lastestRevision) {
				from = lastestRevision;
			}

			if (size == 0 || size > 50) {
				size = DEFAULT_PAGE_SIZE;
			}

			if (to > 0) {
				revisionInfoList = client.getRevisionInfoListFromLast(to, size);
			} else {
				revisionInfoList = client.getRevisionInfoListToBase(from, size);
			}
		}

		return LIST;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setFrom(long from) {
		this.from = from;
	}

	public void setTo(long to) {
		this.to = to;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public Component getComponent() {
		return component;
	}

	public List<RevisionInfo> getRevisionInfoList() {
		return revisionInfoList;
	}

	public void setDashboardBO(DashboardBO dashboardBO) {
		this.dashboardBO = dashboardBO;
	}

	public void setSubversionClientFactory(SubversionClientFactory subversionClientFactory) {
		this.subversionClientFactory = subversionClientFactory;
	}

}
